/*
 *@description 路由转换--面包屑自动转换只支持到3层,如果有超过3层,请自己配置面包屑
 * @author yx
*/
import menu from '../menu.js'
const route = []
let breadcrumb = []
const transform = (menu) => {
  menu.forEach((element, index) => {
    if (element.children) {
      const { path, children, text, breadcrumbJump = true } = element || {}
      const obj = { text: text }
      path && breadcrumbJump && Object.assign(obj, { path: path })
      breadcrumb.push(obj) // 增加父面包屑
      menuTransformRouter(children, menu[index])
    } else {
      const { path, name, text } = element || {}
      const breadcrumbs = JSON.parse(JSON.stringify(breadcrumb))
      const obj = { text: text }
      path && Object.assign(obj, { path: path })
      breadcrumbs.push(obj)
      route.push({
        path: path,
        name: name,
        component: () => import('@/views' + path + '.vue'),
        meta: { breadcrumb: element.breadcrumb ? element.breadcrumb : breadcrumbs }
      })
    }
  })
}

// leagueMenu 最顶层的菜单数据
const menuTransformRouter = (menu, leagueMenu) => {
  menu.forEach((element, index, arr) => {
    if (element.children) {
      const { path, children, text, breadcrumbJump = true } = element || {}
      const obj = { text: text }
      path && breadcrumbJump && Object.assign(obj, { path: path })
      breadcrumb.push(obj) // 增加父面包屑
      judgeRepeat(breadcrumb, leagueMenu)
      menuTransformRouter(children, leagueMenu)
    } else {
      const { path, name, text } = element || {}
      const breadcrumbs = JSON.parse(JSON.stringify(breadcrumb))
      const obj = { text: text }
      path && Object.assign(obj, { path: path })
      breadcrumbs.push(obj)
      route.push({
        path: path,
        name: name,
        component: () => import('@/views' + path + '.vue'),
        meta: { breadcrumb: element.breadcrumb ? element.breadcrumb : breadcrumbs }
      })
      if (index === arr.length - 1) {
        breadcrumb = []
      }
    }
  })
  return route
}

// 判断添加的面包屑是否重复,并增加顶层面包屑
const judgeRepeat = (breadcrumb, leagueMenu) => {
  const { text, path } = leagueMenu || {}
  const obj = { text: text }
  let flag = true
  path && Object.assign(obj, { path: path })
  breadcrumb.some(item => {
    if (item.text === text) {
      flag = false
      return true
    }
  })
  flag && breadcrumb.unshift(obj)
  console.log(breadcrumb, leagueMenu, 1)
  return breadcrumb
}
transform(menu)
export default route
